//index.js
const app = getApp()
const customRequst = require('../../common/customRequst.js');
const customUser = require('../../common/commonUser');
Page({
  data: {
    active: 0,
    periList: [],
    tabList: [],
    recomList: [],
    inforList: [],
    show: false,
    isShow: false,
    searchData: {
      "page": 1,
      "pagesize": "15",
      "cate_id": "base",
    },
  },
  onLoad: function (options) {
    //判断是否来自分享
    if (JSON.stringify(options) != "{}" && options != undefined && options != null) {
      let urls = decodeURIComponent(options.q);
      //urls = "https://www.wzqai.com/userShare?user_id=1";测试连接
      let aryOptions = urls.split('?');
      if (aryOptions) {
        let urlParams = aryOptions[1];
        let aryParams = urlParams.split('&');
        if (aryParams) {
          for (let i in aryParams) {
            let aryP = aryParams[i].split('=');
            if (aryP) {
              if (aryP[0] == 'user_id') {
                console.log(aryP[1]);
                wx.setStorageSync('share_parent_id', aryP[1]);
              }
              if (aryP[0] == 'admin_id') {
                console.log(aryP[1]);
                wx.setStorageSync('share_admin_id', aryP[1]);
              }
            }
          }
        }
      }
    }


    // 页面出现在前台时执行
    let that = this;
    that.data.periList = [];
    that.data.searchData.pagesize = 5;
    that.data.searchData.page = 1;
    that.onloadPeriList();
    that.getTablist()
    that.getRecomList()
    that.getInfoList()
  },
  onShow: function (options) {
    app.globalData.Chat.windowsobj = this;
    if (!app.globalData.Chat.serviceConfig.isWebScoket) {
      let userIsLogin = customUser.getUserToken();
      if (userIsLogin) {
        app.globalData.Chat.init();
      }
    }

  },

  onReachBottom: function () {
    // 页面触底时执行
    let that = this;
    // that.data.searchData.page = that.data.searchData.page + 1;
    // that.onloadPeriList();
  },

  //书刊列表数据
  onloadPeriList() {
    let that = this;
    customRequst.postRequest('api/peri/list', that.data.searchData).then(res => {
      if (res.code == 1) {
        that.setData({
          periList: res.data
        })
      }
    })
  },
  getInfoList() {
    let that = this;
    customRequst.postRequest('api/sysnotice/list', ).then(res => {
      if (res.code == 1) {
        that.setData({
          inforList: res.data
        })
      }
    })
  },
  // tab切换数据
  getTablist() {
    let that = this;
    customRequst.postRequest('api/cate/list', ).then(res => {
      if (res.code == 1) {
        that.setData({
          tabList: res.data,
          show: true
        })
      }
    })
  },
  // 推荐列表
  getRecomList() {
    let that = this;
    customRequst.postRequest('api/peri/gethometjlist', ).then(res => {
      if (res.code == 1) {
        that.setData({
          recomList: res.data
        })
      }
    })
  },
  //列表点击
  onPeriItem(event) {
    let id = event.currentTarget.dataset.id;
    let url = '/pages/peris/detail/index?id=' + id;
    wx.navigateTo({
      url: url
    });
  },
  // 切换点击
  onChange(e) {
    this.data.searchData.cate_id = e.detail.name
    this.onloadPeriList()
  },

  //跳转书刊列表
  getPerisList: function (e) {
    let cate_id = e.currentTarget.dataset.cate_id;
    app.globalData.globalCateId = cate_id;
    wx.switchTab({
      url: '/pages/peris/index/index',
    });
  },

  //加载聊天界面
  getChats: function (e) {
    let that = this;
    if (!that.isUserLogin()) {
      return;
    }
    let bus_id = e.currentTarget.dataset.peris_id;
    wx.navigateTo({
      url: '/pages/chat/chat?peris_id=' + bus_id,
    });
  },

  //前往分享界面
  getShareImages: function (e) {
    wx.navigateTo({
      url: '/pages/promote/promote',
    });
  },

  // 公告详情
  toNotInfo(e) {
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: `/pages/notice/index?id=${e.currentTarget.dataset.id}`,
    });
  },
  //判断是否登录
  isUserLogin: function () {
    let that = this;
    let userInfo = customUser.getUserInfo();
    if (!userInfo) {
      wx.showModal({
        title: '登录提示',
        content: '您还未登录，请先登录',
        success: function (res) {
          if (res.confirm) {
            let url = '/pages/index/index';
            wx.navigateTo({
              url: '/pages/users/login/index?url=' + url
            })
          }
        }
      });
      return false;
    } else {
      return true;
    }
  },

  handleNewMessage: function (msg) {
    wx.showTabBarRedDot({
      index: 2,
    })
  },
  //处理聊天返回的数据
  handleChatMag: function (msg) {
    switch (msg.msgtype) {
      case 'new_message': //收到新消息
        wx.showTabBarRedDot({
          index: 2,
        });
        break;
      case "push_sys_message":
        wx.showTabBarRedDot({
          index: 2,
        });
        break;
    }
  },
  // 快捷找刊
  showview() {
    let that = this
    that.setData({
      isShow: true
    })
  },
  close() {
    let that = this
    that.setData({
      isShow: false
    })
  },
  // 弹出层选中
  clickItem(e) {
    let that = this
    that.setData({
      activeItem: e.currentTarget.dataset.id,
    })
    that.data.searchData.cate_id = e.currentTarget.dataset.id
    this.onloadPeriList()
  },
  reset() {
    let that = this
    that.setData({
      activeItem: 0
    })
  },
});